package com.dt.dubai.core.qrcode.controller;

import org.apache.commons.lang3.StringUtils;
import org.krysalis.barcode4j.impl.codabar.CodabarBean;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean;
import org.krysalis.barcode4j.impl.pdf417.PDF417Bean;
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
import org.krysalis.barcode4j.impl.upcean.UPCABean;
import org.krysalis.barcode4j.impl.upcean.UPCEBean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 生成条形码
 */


@RequestMapping("/barCodeController")
@RestController
public class barCodeController {
    /**
     *具体实现 Code 128
     * @param msg
     * @param path
     */
    @GetMapping("/getBarCode128")
    @ResponseBody
    public void getBarCode128(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            Code128Bean bean=new Code128Bean();
            //设置长宽
            final int resolution=200;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 EAN-13
     * @param msg
     * @param path
     */
    @GetMapping("/getBarEAN13")
    @ResponseBody
    public void getBarEAN13(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            EAN13Bean bean=new EAN13Bean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 PDF417
     * @param msg
     * @param path
     */
    @GetMapping("/getBarPDF417")
    @ResponseBody
    public  void getBarPDF417(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            PDF417Bean bean=new PDF417Bean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 Codabar
     * @param msg
     * @param path
     */
    @GetMapping("/getBarCodabar")
    @ResponseBody
    public  void getBarCodabar(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)

            CodabarBean bean=new CodabarBean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 Interleaved 2 of 5
     * @param msg
     * @param path
     */
    @GetMapping("/getBarInterleaved2Of5")
    @ResponseBody
    public  void getBarInterleaved2Of5(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)

            Interleaved2Of5Bean bean=new Interleaved2Of5Bean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 UPCA
     * @param msg
     * @param path
     */
    @GetMapping("/getBarUPCA")
    @ResponseBody
    public void getBarUPCA(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)

            UPCABean bean=new UPCABean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 UPCE
     * @param msg
     * @param path
     */
    @GetMapping("/getBarUPCE")
    @ResponseBody
    public void getBarUPCE(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            UPCEBean bean=new UPCEBean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth=0.20;
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

